<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <div>
            <input type="text" v-model="todoValue">
            <button @click="handle">提交</button>
        </div>
        <ul>
            <!-- <li v-for="item in list ">{{item}}</li> -->
            <todo-item :content="item" :index="index" v-for="(item,index) in list" @delete="handleItemdelete"><!--使用 TodoItem --><!-- 父组件 -->
                    <!-- 父组件向子组件传值 -->
            </todo-item>
        </ul>
    </div>
    <script>
        /* Vue.component("TodoItem",{//全局组件
            props:['content'],//外部数据
            template:"<li>{{content}}</li>"//子组件模板
        }) */
        var TodoItem={//局部组件，必须注册到Vue中//子组件
            props:['content','index'],//外部数据
            template:"<li @click='handleItemclick'>{{content}}</li>",//子组件模板
            methods:{
                handleItemclick:function(){
                    this.$emit("delete",this.index)
                    //通过$emit实现子组件向父组件传值
                }
            }
        }
        var app=new Vue({
            el:"#app",
            components:{//局部组件注册到Vue中
                TodoItem:TodoItem
            },
            data:{
                todoValue:"",
                list:[]
            },
            methods:{
                handle:function(){
                    this.list.push(this.todoValue),
                    this.todoValue=''
                },
                handleItemdelete:function(index){
                    this.list.splice(index,1)
                }
            }           
        })
            setTimeout(function () {
                app.$data.content="this enough"  //$data指向data对象内容             
            },2000)
    </script>
</body>
</html>